package com.hiv.mapper;

import com.hiv.mapper.domain.AdminRole;
import com.hiv.mapper.domain.Role;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;


/**
 * permisson
 */
@Mapper
@Repository
public interface AdminRoleMapper {
    /**
     * 增(批量插入)
     */
    @Select({"<script>",
            "insert into admin_role(admin_id, role_id, operator) values ",
            " <foreach collection=\"adminRoles\" item=\"item\" separator=\",\"> ",
            "  (#{item.adminId}, #{item.roleId}, #{item.operator})",
            " </foreach> ",
            "</script>"})
    void batchInsert(@Param("adminRoles") List<AdminRole> adminRoles);

    /**
     * 删
     */
    @Delete("delete from admin_role where admin_id = #{adminId}")
    void deleteByAdminId(@Param("adminId") long adminId);

    /**
     * 查
     */
    @Select("select role.* from admin_role right JOIN role on admin_role.role_id = role.id where admin_role.admin_id = #{adminId} ")
    List<Role> findByAdminId(@Param("adminId") long adminId);

    @Select("select count(1) from admin_role where role_id = #{roleId}")
    long countByRoleId(@Param("roleId") long roleId);

    @Select("select count(1) from admin_role where admin_id=#{adminId} and role_id=#{roleId}")
    Long findByAdminIdAndRoleId(@Param("adminId") long adminId, @Param("roleId") Long roleId);
}